package com.chen.controller;

import com.chen.entity.CTTest;
import com.chen.mapper.CTTestMapper;
import com.chen.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
@RequestMapping(value = "/test")
public class CTTestController {

    @Autowired
    private CTTestMapper ctTestMapper;

    // http://127.0.0.1:8080/mysql/test/list
    @GetMapping(value = "/list")
    public R list(){
        return R.success(ctTestMapper.selectList(null));
    }

    // http://127.0.0.1:8080/mysql/test/detail?id=
    @GetMapping(value = "/detail")
    public R detail(String id){
        return R.success(ctTestMapper.selectById(id));
    }

    // http://127.0.0.1:8080/mysql/test/insert
    @GetMapping(value = "/insert")
    public R insert(){
        String id = UUID.randomUUID().toString().replace("-","").substring(0,10);
        CTTest ctTest = new CTTest("王五","镇海");
        return R.success(ctTestMapper.insert(ctTest));
    }

    // http://127.0.0.1:8080/mysql/test/update?id=1
    @GetMapping(value = "/update")
    public R update(String id){
        CTTest ctTest = new CTTest(id,"陈一","宁波");
        int i = ctTestMapper.updateById(ctTest);
        if (i==0){
            return R.success("更新失败！");
        }
        return R.success("更新成功！");
    }

    // http://127.0.0.1:8080/mysql/test/delete?id=1
    @GetMapping(value = "/delete")
    public R delete(String id){
        int i = ctTestMapper.deleteById(id);
        if (i==0){
            return R.success("删除失败！");
        }
        return R.success("删除成功！");
    }
}
